package com.demomaster.quickcompat.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;

public class QuickStateListDrawable extends StateListDrawable implements QuickDrawable {
    float[] mRadii;
    float mRadius = 0;

    public QuickStateListDrawable() {
    }

    public QuickStateListDrawable(Drawable original) {
        if (original instanceof StateListDrawable) {
            // 获取原始 StateListDrawable 的状态数量
            int stateCount = 0;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
                stateCount = ((StateListDrawable) original).getStateCount();
                for (int i = 0; i < stateCount; i++) {
                    // 获取每个状态集及其对应的 Drawable
                    int[] stateSet = ((StateListDrawable) original).getStateSet(i);
                    if (stateSet == null) continue;
                    Drawable drawableCopy = copyDrawable(original.getCurrent());
                    // 设置边界（如果需要的话）
                    Rect bounds = original.getBounds();
                    if (bounds != null) {
                        drawableCopy.setBounds(bounds);
                    }

                    // 将复制的 Drawable 添加到新的 StateListDrawable 中
                    addState(stateSet, drawableCopy);
                }
            }
        }
    }

    private Drawable copyDrawable(Drawable original) {
        if (original instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) original;
            return new ColorDrawable(colorDrawable.getColor());
        } else if (original instanceof GradientDrawable) {
            GradientDrawable gradientDrawable = (GradientDrawable) original;
            GradientDrawable copy = new GradientDrawable();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                copy.setShape(gradientDrawable.getShape());
                copy.setColor(gradientDrawable.getColor().getDefaultColor());
                copy.setCornerRadius(gradientDrawable.getCornerRadius());
            }
            // 如果有其他属性需要复制，也在这里添加相应的设置
            return copy;
        } else {
            // 对于其他类型的 Drawable，您可以根据需要添加更多的处理逻辑
            // 或者返回原始 Drawable 的常量状态的新实例
            return original;
            //return original.getConstantState().newDrawable();
        }
    }

    @Override
    public void setCornerRadii(float[] radii) {
        // super.setCornerRadii(radii);
        this.mRadii = radii;
    }

    public float[] getCornerRadii() {
        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.P) {
            return getCornerRadii();
        } else {
            return null;
        }
    }

    @Override
    public void setCornerRadius(float radius) {
        //super.setCornerRadius(radius);
        mRadius = radius;
    }

    Paint mStrokePaint;
    ColorStateList mStrokeColors;
    int mStrokeWidth;

    @Override
    public void setStroke(int borderWidth, ColorStateList borderColor) {
        mStrokePaint = new Paint();
        mStrokeColors = borderColor;
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(borderWidth);
        mStrokeWidth = borderWidth;
    }

    public float getCornerRadius() {
        return mRadius;
    }

    @Override
    public void draw(Canvas canvas) {
        Rect rect = getBounds();
        RectF rectF = new RectF(rect); // 将 Rect 转换为 RectF
        Path path = new Path();
        path.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CCW);
        if (mRadii != null) {
            path.addRoundRect(rectF, mRadii, Path.Direction.CW);
        }
        canvas.clipPath(path);
        super.draw(canvas);
        if (mStrokePaint != null) {
            canvas.drawPath(path, mStrokePaint);
        }
    }

    @Override
    public void addState(int[] stateSet, Drawable drawable) {
// 将传入的 drawable 转换为 GradientDrawable
        Drawable convertedDrawable = convertToGradientDrawable(drawable);
        if (convertedDrawable instanceof QuickDrawable) {
            if (mRadii != null) {
                ((QuickDrawable) convertedDrawable).setCornerRadii(mRadii);
            }
            if (mRadius != 0) {
                ((QuickDrawable) convertedDrawable).setCornerRadius(mRadius);
            }
        } else if (convertedDrawable instanceof GradientDrawable) {
            if (mRadii != null) {
                ((GradientDrawable) convertedDrawable).setCornerRadii(mRadii);
            }
            if (mRadius != 0) {
                ((GradientDrawable) convertedDrawable).setCornerRadius(mRadius);
            }
        }

        super.addState(stateSet, drawable);
    }

    private Drawable convertToGradientDrawable(Drawable drawable) {
        if (drawable instanceof QuickDrawable) {
            return drawable; // 如果已经是 GradientDrawable，则直接返回
        } else if (drawable instanceof GradientDrawable) {
            return drawable; // 如果已经是 GradientDrawable，则直接返回
        } else {
            // 创建一个新的 GradientDrawable 并复制原始 drawable 的颜色或背景
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);

            if (drawable instanceof ColorDrawable) {
                gradientDrawable.setColor(((ColorDrawable) drawable).getColor());
            } else if (drawable instanceof BitmapDrawable) {
                // 如果是图片类型的 Drawable，可以考虑设置为背景图等
                // 这里简单处理，仅设置颜色为透明
                gradientDrawable.setColor(Color.TRANSPARENT);
                QuickBitmapDrawable quickBitmapDrawable = new QuickBitmapDrawable(((BitmapDrawable) drawable).getBitmap());
                return quickBitmapDrawable;
            } else {
                // 对于其他类型的 Drawable，这里只简单地设置一个默认颜色
                gradientDrawable.setColor(Color.GRAY);
            }

            return gradientDrawable;
        }
    }
}
